package com.pxxy.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.pxxy.mall.product.dto.attr.AttrReqDTO;
import com.pxxy.mall.product.entity.ProductAttrValueEntity;
import com.pxxy.mall.product.vo.AttrResVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pxxy.mall.product.service.AttrService;
import com.pxxy.common.utils.PageUtils;
import com.pxxy.common.utils.R;


/**
 * 商品属性
 *
 * @author Zhangjj
 * @email zhangjianjian@gmail.com
 * @date 2023-11-29 17:05:08
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查看规格参数/销售属性分页
     *
     * @param params
     * @return
     */
    @GetMapping("/{attrType}/list/{catId}")
    public R baseList(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId, @PathVariable("attrType") String type) {
        PageUtils page = attrService.queryAttrPage(params, catId, type);
        return R.ok().put("page", page);
    }

    /**
     * 获取spu规格
     *
     * @param spuId
     * @return
     */

    @GetMapping("/base/listforspu/{spuId}")
    public R baseListforspu(@PathVariable("spuId") Long spuId) {
        return attrService.getBaseListforspu(spuId);

    }


    /**
     * 查询属性详情
     */
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrResVo attr = attrService.getDetail(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody AttrReqDTO reqDTO) {
        attrService.saveAttr(reqDTO);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AttrReqDTO reqDTO) {
        attrService.updateAttrDetail(reqDTO);

        return R.ok();
    }


    /**
     * 修改商品规格
     */
    @PostMapping("/update/{spuId}")
    public R updateSpu(@RequestBody List<ProductAttrValueEntity> productAttrValueEntities,@PathVariable("spuId")Long spuId) {
        attrService.updateAttrSpu(productAttrValueEntities,spuId);

        return R.ok();
    }




    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
